package com.qingsong.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.qingsong.dto.*;
import com.qingsong.entity.Role;
import com.qingsong.excel.RoleExcel;
import com.qingsong.service.RoleService;
import com.qingsong.util.EasyExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author qingsong
 */
@Tag(name="角色模块")
@RestController
@RequestMapping("/api/v1/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @Operation(summary = "单增一条记录")
    @PostMapping("save")
    public Object save(@RequestBody @Validated RoleInsertDTO roleInsertDTO){
        return roleService.insert(roleInsertDTO);
    }

    @Operation(summary = "单查一条记录")
    @GetMapping("getInfo/{id}")
    public Object getInfo(@PathVariable Long id){
        return roleService.selectById(id);
    }

    @Operation(summary = "修改一条记录")
    @PostMapping("update")
    public Object update(@RequestBody @Validated RoleUpdateDTO roleUpdateDTO){
        return roleService.updateById(roleUpdateDTO);
    }

    @Operation(summary = "删除一条记录")
    @PostMapping ("/remove/{id}")
    public Object remove(@PathVariable Long id){
        return roleService.logicDeleteById(id);
    }

    @Operation(summary = "批量删除多条记录")
    @PostMapping ("removeBatch")
    public Object removeBatch(@RequestParam List<Long> ids){
        return roleService.logicDeleteByIds(ids);
    }

    @Operation(summary = "查询全部记录")
    @GetMapping ("list")
    public Object list(){
        return roleService.list();
    }

    @Operation(summary = "分页查询记录")
    @PostMapping ("page")
    public Object page(@RequestBody @Validated PageDTO pageDTO){
        return roleService.page(pageDTO);
    }

    @Operation(summary = "打印数据报表")
    @SneakyThrows
    @GetMapping("/excel")
    public void excel(HttpServletResponse resp) {
        List<RoleExcel> roleExcels = new ArrayList<>();
        List<Role> roles = roleService.list();
        for (Role role : roles) {
            RoleExcel roleExcel = BeanUtil.copyProperties(role, RoleExcel.class);
            roleExcels.add(roleExcel);
        }
        EasyExcelUtil.download(resp, "角色统计表.xlsx", "sheet01",
                RoleExcel.class, roleExcels);
    }

    @Operation(summary = "按角色名搜索")
    @PostMapping("pageLikeTitle")
    public Object pageLikeTitle(@RequestBody @Validated RolePageDTO dto){
        return roleService.pageLikeTitle(dto);
    }

    @Operation(summary = "我的权限列表")
    @GetMapping ("getPermissions/{id}")
    public Object getPermissions(@PathVariable Long id){
        return roleService.getPermissions(id);
    }

    @Operation(summary = "修改权限列表")
    @PostMapping("updatePermissions")
    public Object updatePermissions(@RequestBody RoleUpdatePermissionsDTO dto){
        return roleService.updatePermissions(dto);
    }
}
